package com.edais.service.imp;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

import com.edais.domain.*;
import com.edais.util.*;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.edais.dao.CenterDao;
import com.edais.dao.OptionDao;
import com.edais.dao.UserDao;
import com.edais.mina.TcpMinaClient;
import com.edais.service.CenterService;
import com.edais.service.LoanService;
import com.edais.service.OrderService;
import com.edais.service.RateRisesCouponsService;
import com.edais.service.SystemIdWorkerService;
import com.edais.vo.BankVo;
import com.edais.vo.OrderListVo;
import com.edais.vo.ReadyLoanVo;
import com.edais.vo.UserBankCardVo;

import freemarker.template.Configuration;
import freemarker.template.Template;

@Service

public class CenterServiceImp implements CenterService {
	private static final Log logger = LogFactory.getLog(CenterServiceImp.class);

	@Autowired
	private CenterDao centerDao;
	@Autowired
	private OptionDao optionDao;
	@Autowired
	private UserDao usersDao;
	@Autowired
	private OrderService orderService;
	@Autowired
	private LoanService loanService;
	@Autowired
	private RateRisesCouponsService rateRisesCouponsService;
	@Autowired
	private SystemIdWorkerService systemIdWorkerService;

	/**
	 * 查询回款卡号
	 * 
	 * */
	@Override
	public UserBankCardVo getRecePayBank(Map<String,Object> map) throws Exception{
		List<UserRecePayCard> bankCardList = this.centerDao.getRecePayBank(map);
		UserBankCardVo userBankCardVo = null;
		for (UserRecePayCard userRecePayCard : bankCardList) {
			userBankCardVo = new UserBankCardVo();
			userBankCardVo.setAccount_name(userRecePayCard.getAccount_name());// 开户名
			userBankCardVo.setCard_number(userRecePayCard.getCard_number());// 银行卡号说
			userBankCardVo.setId(String.valueOf(userRecePayCard.getId()));// 银行卡ID
			userBankCardVo.setPhone_no(null == userRecePayCard.getPhone_no() ? "" : userRecePayCard.getPhone_no());// 预留手机号
			userBankCardVo.setBank_name(userRecePayCard.getBank_name());// 银行名称
			userBankCardVo.setBank_id(userRecePayCard.getBank_id()+"");// 银行id
			userBankCardVo.setCityName(null == userRecePayCard.getCity_name() ? "": userRecePayCard.getCity_name());//城市名称
			userBankCardVo.setPicturePath(userRecePayCard.getPicture());//银行卡log 存储路径
			userBankCardVo.setSingleDayLimit(null == userRecePayCard.getSingle_day_limit() ? "": userRecePayCard.getSingle_day_limit());//单日限额
			userBankCardVo.setSingleMonthLimit(null == userRecePayCard.getSingle_month_limit() ? "": userRecePayCard.getSingle_month_limit());//单月限额
			userBankCardVo.setSingleLimit(null == userRecePayCard.getSingle_limit() ? "": userRecePayCard.getSingle_limit());//单日限额

			break;
		}
		return userBankCardVo;
	}

	@Override
	public UserBankCardVo getRecePayRouteBank(Map<String, Object> map) throws Exception {
		List<UserRecePayCard> bankCardList = this.centerDao.getRecePayBank(map);
		UserBankCardVo userBankCardVo = null;
		for (UserRecePayCard userRecePayCard : bankCardList) {

			//查询路由
			Map<String,Object> routeMap = new HashMap<String, Object>();
			routeMap.put("bankCode",userRecePayCard.getYlbankcode());
			ThirdProductRoute route = this.centerDao.getRouteBankByCode(routeMap);
			if(null == route)
				break;
			userBankCardVo = new UserBankCardVo();
			userBankCardVo.setAccount_name(userRecePayCard.getAccount_name());// 开户名
			userBankCardVo.setCard_number(userRecePayCard.getCard_number());// 银行卡号说
			userBankCardVo.setId(String.valueOf(userRecePayCard.getId()));// 银行卡ID
			userBankCardVo.setPhone_no(null == userRecePayCard.getPhone_no() ? "" : userRecePayCard.getPhone_no());// 预留手机号
			userBankCardVo.setBank_name(userRecePayCard.getBank_name());// 银行名称
			userBankCardVo.setBank_id(userRecePayCard.getBank_id()+"");// 银行id
			userBankCardVo.setCityName(null == userRecePayCard.getCity_name() ? "": userRecePayCard.getCity_name());//城市名称
			userBankCardVo.setPicturePath(userRecePayCard.getPicture());//银行卡log 存储路径
			userBankCardVo.setSingleDayLimit(route.getBankDayLimit());//单日限额
			userBankCardVo.setSingleMonthLimit(route.getBankMonthLimit());//单月限额
			userBankCardVo.setSingleLimit(route.getBankLimit());//单日限额
			userBankCardVo.setSingleMinLimit(route.getBankMinLimit());//单笔最小限额
			break;
		}
		return userBankCardVo;
	}

	/**
	 * 获取系统支持的银行列表
	 * 
	 * 
	 * */
	@Override
	public List<BankVo> getBanks(Map<String,Object> bankParam) throws Exception{
		List<BankVo> bankVoList = new ArrayList<BankVo>();
		
		List<Bank> bankList = optionDao.getBankDetail(bankParam);
		for (Bank bank : bankList) {
			BankVo bankVo = new BankVo();
			bankVo.setId(String.valueOf(bank.getId()));  // id
			bankVo.setBankCode(bank.getBank_code());     // 银行代码
			bankVo.setBankName(bank.getBank_name());     // 银行名称

			bankVoList.add(bankVo);
		}
		return bankVoList;
	}

	/**
	 * 获取鼓钱包标的列表
	 * 
	 * 
	 * */
	@Override
	@Transactional(propagation=Propagation.SUPPORTS,readOnly=true)
	public Map<String, Object> getReadyLoan(String loanStatus, String loantype, 
												Integer current, Integer pageSize) throws Exception {
		Map<String, Object> rtnMap = new HashMap<String, Object>();
		List<ReadyLoanVo> readyLoanVoList = new ArrayList<ReadyLoanVo>();
		
		String rowBegin = String.valueOf((current - 1) * pageSize + 1);
		String rowEnd = String.valueOf(current * pageSize);
		
		// 获取鼓钱包标的招标中总记录数
		Integer totalNum = Integer.parseInt(this.centerDao.getReadyLoanTotalNum(loanStatus, loantype));
		
		// 标的总记录数 > 0
		if (totalNum > 0) {
			// 获取鼓钱包招标中标的列表
			List<Loan> loanList = this.centerDao.getReadyLoan(loanStatus, loantype, rowBegin, rowEnd);
			
			for (Loan loan : loanList) {
				ReadyLoanVo readyLoanVo = new ReadyLoanVo();
				readyLoanVo.setLoanId(String.valueOf(loan.getId()));   // 标的id
				readyLoanVo.setLoanState("招标中"); // 标的状态：招标中
				readyLoanVo.setLoanAmount(String.valueOf(loan.getContactAmount())); // 借款金额（9990.00元）
				readyLoanVo.setInterest(String.valueOf(loan.getInterest()));  // 年化利率（%）
				readyLoanVo.setLoanPeriods(String.valueOf(loan.getLoanMonths()));   // 贷款期数
				readyLoanVo.setOpenTime(loan.getOpenTime());   //招标日期(yyyy/MM/dd)

				readyLoanVoList.add(readyLoanVo);
			}
		}
		
		rtnMap.put("totalNum", totalNum);
		rtnMap.put("listSize", String.valueOf(readyLoanVoList.size()));
		rtnMap.put("list", readyLoanVoList);
		
		return rtnMap;
	}

//	/**
//	 * 充值订单生成
//	 * 
//	 * 
//	 * */
//	@Override
//	@Transactional
//	public void createRechargeOrder(OrderFormRecharge orderFormRecharge)
//			throws Exception {
//		this.centerDao.createRechargeOrder(orderFormRecharge);
//	}
//	
//	/**
//	 * 获取SEQ_CYCLE_ORDER_NO的seq值
//	 * 
//	 * 
//	 * */
//	@Override
//	@Transactional
//	public int getSeqCycleOrderNo()throws Exception{
//		return this.centerDao.getSeqCycleOrderNo();
//	}

	/**
	 * 根据订单号取得用户充值的基本信息
	 * 
	 * 
	 * */
	@Override
	public OrderFormRecharge selectOrderFromRechargeByOrderId(String orderId)throws Exception{
		return this.centerDao.selectOrderFromRechargeByOrderId(orderId);
	}

//	/**
//	 * 更新充值订单状态
//	 * 
//	 * 
//	 * */
//	@Override
//	@Transactional
//	public void updateOrderFromRecharge(
//			OrderFormRecharge orderFormRecharge) throws Exception {
//		this.centerDao.updateOrderFromRecharge(orderFormRecharge);	
//	}
	
	/**
	 * 调用核心接口
	 * 
	 * */
	@Override
	@Transactional
	public Map coreUse(String userId,String transCode,Map<String, Object> pageModel) throws Exception {
		// 设置报文体报文数据
		String jsonStr = CommonUtil.constructJsonStr(transCode, userId, pageModel);// 构建json字符串
		TcpMinaClient tcpMinaClient = new TcpMinaClient();
		String rtnStr = tcpMinaClient.sendMessage(jsonStr);

		if(StringUtils.isBlank(rtnStr)){
			logger.debug("调用核心接口发生异常" + ",接口id：" + transCode);
			throw new Exception("调用核心接口发生异常" + ",接口id：" + transCode);
		}
		
		rtnStr = rtnStr.substring(8, rtnStr.length());
		// 转化json数据为页面模型数据格式
		Map rtnMap = (HashMap) JsonUtil.getObjectFromJsonString(rtnStr,
				HashMap.class);

		return rtnMap;
	}
	
	/**
	 * 判断购买的理财计划是否在购买期限内
	 * 
	 * 
	 * */
	@Override
	public String checkPlanSellTime(String id) throws Exception{
		// 理财计划详情获取
		Finance finance = this.centerDao.getFinanceDetail(id,"Y");
		
		if(null != finance){
			return "true";
		}else{
			return "plan.has.not.opened";
		}
	}
	/**
	 * 获取理财产品的具体信息
	 * 
	 * 
	 * */
	@Override
	public Finance getFinanceplanDetail(String id,String flag) throws Exception{
		return this.centerDao.getFinanceDetail(id,flag);
	}
	
	/**
	 * 根据用户ID查询用户绑定的邀请码相关信息
	 * 返回Map说明：
	 * INVITER_INVITATION_CODE:用户绑定的邀请码
	 * ENABLE：渠道是否有效
	 * VALID_STATUS：绑定邀请码所属用户是否有效
	 * */
	@Override
	public Map<String, String> getInviteCodeMsg(String userID)
			throws Exception {
		Map<String,String> retMap = new HashMap<String,String>();
		List<Map<String, String>> inviteCodeMsgList = new ArrayList();
		inviteCodeMsgList = this.centerDao.getInviteCodeMsg(userID);
		if(inviteCodeMsgList==null||inviteCodeMsgList.size()==0
				||(inviteCodeMsgList.size()==1&&inviteCodeMsgList.get(0)==null)){
			return null;
		}else{
			retMap.put("INVITER_INVITATION_CODE", inviteCodeMsgList.get(0).get("INVITER_INVITATION_CODE"));
			for(int i=0;i<inviteCodeMsgList.size();i++){
				Map<String,String> tempMap = inviteCodeMsgList.get(i);
				String channelEnable = tempMap.get("ENABLE");
				String userEnable = tempMap.get("VALID_STATUS");
				if("Y".equalsIgnoreCase(userEnable)){
					retMap.put("VALID_STATUS", "Y");
					retMap.put("ENABLE", channelEnable);
					return retMap;
				}
			}
			for(int i=0;i<inviteCodeMsgList.size();i++){
				Map<String,String> tempMap = inviteCodeMsgList.get(i);
				String channelEnable = tempMap.get("ENABLE");
				if("Y".equalsIgnoreCase(channelEnable)){
					retMap.put("VALID_STATUS", "N");
					retMap.put("ENABLE", "Y");
					return retMap;
				}
			}
			retMap.put("VALID_STATUS", "N");
			retMap.put("ENABLE", "N");
			return retMap;
		}
	}
	/**
	 * 验证邀请码相关信息
	 * */
	@Override
	public String checkInviteCode(String inviteCode, String userId)
			throws Exception {
		Map<String,String> inviteCodeMsg = this.getInviteCodeMsg(userId);
		boolean updateFlag = true;		//邀请码是否可修改
		if(inviteCodeMsg!=null){
			String channelEnable = inviteCodeMsg.get("ENABLE");
			String userEnable = inviteCodeMsg.get("VALID_STATUS");
			if("Y".equalsIgnoreCase(channelEnable)){
				if("Y".equalsIgnoreCase(userEnable)){
					updateFlag=false;
				}
			}
		}
		if(inviteCode==null||inviteCode.length()==0){
			if(!updateFlag){
				logger.debug("邀请码不可修改但手机端返回空邀请码！");
				return "N";
			}
		}else{
			if(!updateFlag){
				String s = inviteCodeMsg.get("INVITER_INVITATION_CODE");
				if(!inviteCode.equalsIgnoreCase(s)){
					logger.debug("邀请码不可修改但手机端返回其他邀请码！");
					return "N";
				}
			}
		}
		return "Y";
	}

	/**
	 * invitationCd	注册时绑定的邀请码，后台：如绑定，则显示；如未绑定，则返回空
	 * validStatus  邀请码是否可以修改状态： 0. 可修改 ;1. 不可修改
	 */
	@Override
	public Map<String,Object> getInvitationCdStus(String userId) throws Exception {
		Map<String,Object> jsonMap = new HashMap<String,Object>();
		
		Map<String,String> inviteCodeMsg = this.getInviteCodeMsg(userId);
		if(inviteCodeMsg==null||inviteCodeMsg.size()==0){
			jsonMap.put("invitationCd", "");
			jsonMap.put("validStatus", "0");
		}else{
			//关系表中数据不存在时查询结果为空
			String channelEnable = inviteCodeMsg.get("ENABLE");
			String userEnable = inviteCodeMsg.get("VALID_STATUS");
			//渠道	个人	是否显示
			//有效	有效	反显
			//有效	无效	返回空可修改
			//无效	——	显示且可修改
			if("Y".equalsIgnoreCase(channelEnable)&&"Y".equalsIgnoreCase(userEnable)){
				jsonMap.put("invitationCd", inviteCodeMsg.get("INVITER_INVITATION_CODE"));//邀请码
				jsonMap.put("validStatus", "1");//修改状态
			}else if("Y".equalsIgnoreCase(channelEnable)&&"N".equalsIgnoreCase(userEnable)){
				jsonMap.put("invitationCd", "");
				jsonMap.put("validStatus", "0");
			}else{
				jsonMap.put("invitationCd", inviteCodeMsg.get("INVITER_INVITATION_CODE"));
				jsonMap.put("validStatus", "0");
			}
		}
		return jsonMap;
	}

	
	/** 
	 * 生成订单号
	 */
	@Override
	public String setSeqCycleOrderNo() throws Exception {
		return CommonUtil.createTime() + String.valueOf(centerDao.getSeqCycleOrderNo());
	}

	
	/**
	 * 查询余额(E300012)
	 */
	@Override
	public Map getCusBalance(Map<String,Object> map) throws Exception{
		if(map == null || map.size() == 0){
			return null;
		}
		// 获取用户的基本信息
		String userId = (null == map.get("userId") ? "" : map.get("userId").toString());
		
		String transCode = "E300012";

		// 设置报文体报文数据
		Map<String, Object> pageModel = new HashMap<String, Object>();
		pageModel.put("userId", userId);

		Map resmap = coreUse(userId, transCode, pageModel);
		return resmap;
	}
	
	/**
	 * 更新充值订单状态
	 */
	@Override
	@Transactional
	public boolean updOrderFormRechargeInit(Map<String,Object> map) throws Exception{
		// 1. 更新充值订单状态
		OrderFormRecharge orderFormRecharge = new OrderFormRecharge();

		orderFormRecharge.setOrderform_id(map.get("no_order")+""); // 订单号
		orderFormRecharge.setBack_commen(map.get("info_order")+"");// 反馈描述
		orderFormRecharge.setOrder_id(map.get("oid_paybill")+"");// 关联订单号--返回订单号(第三方返回的流水号)
		orderFormRecharge.setBack_time(new Date());// 反馈时间
		orderFormRecharge.setUpdate_time(new Date()); // 更新时间
		orderFormRecharge.setReceive_message(map.get("reqStr")+"");// 返回报文
//		orderFormRecharge.setSelectbank(ht.get("gate_id"));// 所选银行(银行英文简称)

		// 返回成功,但是数据库不为成功标志,才更新数据库
		if ("TRADE_SUCCESS".equals(map.get("rechargeStatus")+"")) {
			return false;
		}
		
		// 该交易已经更新到数据库中,无需再更新一次
		orderFormRecharge.setRecharge_orderform_status(map.get("status")+"");// 订单状态
		centerDao.updateOrderFromRecharge(orderFormRecharge);
		return true;
	}
	
	
	/**
	 * 充值(E320006)
	 */
	@Override
	public Map recharge(Map<String,Object> map) throws Exception{
		if(map == null || map.size() == 0){
			return null;
		}
		String transCode = "E320006";
		
		String userId = (null == map.get("userId") ? "" : map.get("userId").toString());
		String sum = (null == map.get("sum") ? "" : map.get("sum").toString());
		String orderId = (null == map.get("orderId") ? "" : map.get("orderId").toString());
		
		// 设置报文体报文数据
		Map<String, Object> pageModel = new HashMap<String, Object>();
		pageModel.put("userId", userId);
		pageModel.put("type", "01");
		pageModel.put("accountType", "01");
		pageModel.put("sum", sum);
		pageModel.put("orderId", orderId);

		Map resmap = coreUse(userId, transCode, pageModel);
		return resmap;
		
	}
	
	/**
	 * 购买计划(E300006)
	 */
	@Override
	public Map<String,Object> buyPlan(Map<String,Object> map) throws Exception{
		if(map == null || map.size() == 0){
			return null;
		}
		
		String transCode = "E300006";
		
		String userId = (null == map.get("userId") ? "" : map.get("userId").toString());
		String buyAmount = (null == map.get("buyAmount") ? "" : map.get("buyAmount").toString());
		String planId = (null == map.get("planId") ? "" : map.get("planId").toString());
		String cycleMatchType = (null == map.get("cycleMatchType") ? "" : map.get("cycleMatchType").toString());
		String invitationCode = (null == map.get("invitationCode") ? "" : map.get("invitationCode").toString());
		
		// 设置报文体报文数据
		Map<String, Object> pageModel = new HashMap<String, Object>();
		pageModel.put("userid", userId);
		pageModel.put("amount", buyAmount);// 购买金额
		pageModel.put("currency", "CNY");// 货币单位:CNY人民币，USD美元
		pageModel.put("producttype", planId);// 产品类型
		pageModel.put("cyclematchtype", cycleMatchType);// 资金流转方式：1--收益循环投资—本息循环；2—收益每月返还—本金循环3—不循环
		pageModel.put("buyMedia", "2");//购买媒介:1.网站；2.手机
		pageModel.put("invitationCode", invitationCode);//邀请码
		
		Map resmap = coreUse(userId, transCode, pageModel);
		return resmap;
	}
	

	@Override
	@Transactional
	public Map<String, Object> dealBuyProductByBalance(Map<String, Object> map) throws Exception {
		/**
		 * 余额购买逻辑处理
		 */
		Map<String, Object> retMap = null;
		if(map == null){
			return retMap;
		}
		retMap = new HashMap<String,Object>();
		
		// 购买鼓钱包E300006
		Map<String,Object> resMap = buyPlan(map);
		if (resMap == null || !"00000".equals(resMap.get("rescode"))) {
			logger.error("余额购买产品异常" + resMap.get("resmsg"));
			return null;
		}else{
			// 获取鼓钱包订单号
			String buyOrderId = resMap.get("orderId") + "";
			map.put("buyOrderId", buyOrderId);
			retMap.put("orderId", buyOrderId);
		}

		return retMap;
	}

	/**
	 * 查询该债权的具体信息
	 */
	@Override
	public Map<String,Object> checkLoanStatus(Map<String,Object> map) throws Exception {
		return centerDao.checkLoanStatus(map);
	}
	
	/**
	 * 余额购买散标主处理
	 */
	@Override
	public Map<String, Object> dealBuyBidByBalance(Map<String, Object> map) throws Exception {
		/**
		 * 余额购买逻辑处理
		 */
		Map<String, Object> retMap = null;
		if(map == null){
			return retMap;
		}
		retMap = new HashMap<String,Object>();
		
		// 购买散标E350001
		Map<String,Object> resMap = buyLoan(map);
		if (resMap == null || !"00000".equals(resMap.get("rescode"))) {
			logger.error("余额购买散标异常" + resMap.get("resmsg"));
			return null;
		}else{
			// 获取订单号
			String buyOrderId = resMap.get("orderId") + "";
			map.put("buyOrderId", buyOrderId);
			retMap.put("orderId", buyOrderId);
		}

		return retMap;
	}
	
	/**
	 * 购买债权(E350001)
	 */
	public Map<String,Object> buyLoan(Map<String,Object> map) throws Exception{
		if(map == null || map.size() == 0){
			return null;
		}
		
		String transCode = "E350001";
		
		String userId = (null == map.get("userId") ? "" : map.get("userId").toString());
		String buyAmount = (null == map.get("buyAmount") ? "" : map.get("buyAmount").toString());
		String bidId = (null == map.get("bidId") ? "" : map.get("bidId").toString());
		String cycleMatchType = (null == map.get("cycleMatchType") ? "" : map.get("cycleMatchType").toString());
		String buyMedia = (null == map.get("buyMedia") ? "" : map.get("buyMedia").toString());
		String planId = (null == map.get("planId") ? "" : map.get("planId").toString());
		String loanId = (null == map.get("loanId") ? "" : map.get("loanId").toString());
		//ouyy 增加定期购买生成协议的参数
		String agreementNo=(null == map.get("agreementNo") ? "" : map.get("agreementNo").toString());
		String agreementAutoid=(null == map.get("agreementAutoid") ? "" : map.get("agreementAutoid").toString());
		String assetNo=(null == map.get("assetNo") ? "" : map.get("assetNo").toString());
		String voucherId=(null == map.get("voucherId") ? "" : map.get("voucherId").toString());
		String voucherMoney=(null == map.get("voucherMoney") ? "" : map.get("voucherMoney").toString());
		
		// 设置报文体报文数据
		Map<String, Object> pageModel = new HashMap<String, Object>();
		pageModel.put("userid", userId);
		pageModel.put("amount", buyAmount);// 购买金额
		pageModel.put("currency", "CNY");// 货币单位:CNY人民币，USD美元
		pageModel.put("bidId", bidId);// 标的ID
		pageModel.put("cyclematchtype", cycleMatchType);// 资金流转方式：1--收益循环投资—本息循环；2—收益每月返还—本金循环3—不循环
		pageModel.put("buyMedia", buyMedia);//购买媒介:1.网站；2.手机
		pageModel.put("planId", planId);//理财计划ID
		pageModel.put("loanId", loanId);
		pageModel.put("agreementNo", agreementNo);// 
		pageModel.put("agreementAutoid", agreementAutoid);//
		pageModel.put("assetNo", assetNo);//
		pageModel.put("voucherId", voucherId);//代金券Id
		pageModel.put("voucherMoney", voucherMoney);//代金券金额
		
		Map resmap = coreUse(userId, transCode, pageModel);
		return resmap;
	}

	@Override
	public String checkdepositGetSellTime(String financeID) throws Exception {
		// 
		int res  = this.centerDao.getCountLoan(financeID);
		if(res != 0){
			return "true";
		}else{
			return "scattered.has.not.opened";
		}
	}

	@Override
	public Map<String, Object> getDepositGetDetail(String financeID)
			throws Exception {
		Map<String,Object> getDepositGetDetail = this.centerDao.getDepositGetDetail(financeID);
		return getDepositGetDetail;
	}

	@Override
	public Map dealBuyDepositGetByBalance(Map<String, Object> pageMode)
			throws Exception {
		Map<String, Object> retMap = null;
		if(pageMode == null){
			return retMap;
		}
		retMap = new HashMap<String,Object>();
		
		// 购买鼓钱包E360007
		Map<String,Object> resMap = buyDepositGet(pageMode);
		if (resMap == null || !"00000".equals(resMap.get("rescode"))) {
			logger.error("余额购买产品异常" + resMap.get("resmsg"));
			return null;
		}else{
			// 获取鼓钱包订单号
			String buyOrderId = resMap.get("orderId") + "";
			pageMode.put("buyOrderId", buyOrderId);
			retMap.put("orderId", buyOrderId);
			retMap.put("investAmount", resMap.get("investAmount"));
		}

		return retMap;
	}
	
	public  Map<String,Object> buyDepositGet(Map<String,Object> map) throws Exception{
		if(map == null || map.size() == 0){
			return null;
		}
		
		String transCode = "E360007";
		
		String userId = (null == map.get("userId") ? "" : map.get("userId").toString());
		String buyAmount = (null == map.get("investmentAmount") ? "" : map.get("investmentAmount").toString());
		String planId = (null == map.get("financeId") ? "" : map.get("financeId").toString());
		String cycleMatchType = (null == map.get("cycleMatchType") ? "" : map.get("cycleMatchType").toString());
		String voucherMoney = (null == map.get("voucherMoney") ? "" : map.get("voucherMoney").toString());
		String voucherId = (null == map.get("voucherId") ? "" : map.get("voucherId").toString());

		
		// 设置报文体报文数据
		Map<String, Object> pageModel = new HashMap<String, Object>();
		pageModel.put("userId", userId);
		pageModel.put("investmentAmount", buyAmount);// 购买金额
		pageModel.put("currency", "CNY");// 货币单位:CNY人民币，USD美元
		pageModel.put("financeId", planId);// 产品类型
		pageModel.put("cyclematchtype", cycleMatchType);// 资金流转方式：1--收益循环投资—本息循环；2—收益每月返还—本金循环3—不循环
		pageModel.put("buyMedia", "2");//购买媒介:1.网站；2.手机
		pageModel.put("voucherMoney",voucherMoney);//代金券面额
		pageModel.put("voucherId", voucherId);//代金券ID
		
		Map resmap = coreUse(userId, transCode, pageModel);
		return resmap;
		
	}
	
	
	/**
	 * 更新充值订单的核心状态
	 */
	@Override
	public int updateOrderFromRechargeCoreStatus(
			OrderFormRecharge orderFormRecharge) {
		return this.centerDao.updateOrderFromRechargeCoreStatus(orderFormRecharge);
	}

	@Override
	public Map<String, Object> getAgrnoAndAssno(Integer bidId) throws Exception {
		// TODO Auto-generated method stub
		return this.centerDao.getAgrnoAndAssno(bidId);
	}

	/* (non-Javadoc)
	 * 更新order表的agreementFilename
	 * @see com.edais.service.CenterService#updateOrderFilename(java.util.Map)
	 */
	@Override
	public void updateOrderFilename(Map<String, String> paraMap) {
		// TODO Auto-generated method stub
		/**
		 * 更新order表的agreementFilename
		 */
		Map<String, Object> retMap = null;
		if(paraMap == null){
			return;
		}
		retMap = new HashMap<String,Object>();
		
		// E350007
		String transCode = "E350007";
		// 设置报文体报文数据
		Map<String, Object> pageModel = new HashMap<String, Object>();
		pageModel.put("agreementFilename", null == paraMap.get("agreementFilename") ? "" : paraMap.get("agreementFilename").toString());
		pageModel.put("orderId", null == paraMap.get("orderId") ? "" : paraMap.get("orderId").toString());
		String userId = (null == paraMap.get("userId") ? "" : paraMap.get("userId").toString());
		Map resmap = null;
		try {
			resmap = coreUse(userId, transCode, pageModel);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error("更新order表的agreementFilename发生异常" + e);
			e.printStackTrace();
		}
		if (resmap == null || !"00000".equals(resmap.get("rescode"))) {
			logger.info("更新order表的agreementFilename失败：" + resmap.get("resmsg"));
		}else{
			logger.info("更新order表的agreementFilenam成功");
		}

	}

}
